package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("all")
public class ItemServiceImpl implements ItemService {

    private ProductFeignClient productFeignClient;
    private RedissonClient redissonClient;
    private ThreadPoolExecutor executor;

    @Autowired
    public ItemServiceImpl(ProductFeignClient productFeignClient, RedissonClient redissonClient, ThreadPoolExecutor executor) {
        this.productFeignClient = productFeignClient;
        this.redissonClient = redissonClient;
        this.executor = executor;
    }

    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        Map<String, Object> result = new HashMap<>();

        // TODO: 2023-01-15 暂时注掉,不然影响项目运行,必须得布隆过滤器里面有才能请求到
//        // 判断数据是否存在,布隆过滤器
//        RBloomFilter<Object> bloomFilter = this.redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
//        // 如果查询的 ID 在布隆过滤器中不存在
//        if (!bloomFilter.contains(skuId)) {
//            // 不存在的 skuId,直接返回空值
//            return result;
//        }

        /*
            使用异步编排,将任务按照异步的方式进行执行
         */
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            // 获取实时价格
            BigDecimal skuPrice = this.productFeignClient.getSkuPrice(skuId);
            result.put("price", skuPrice);
        }, executor);

        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = this.productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, executor);

        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            // 获取三级分类
            BaseCategoryView categoryView = this.productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", categoryView);
        }, executor);

        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            // 获取销售属性和选中状态
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = this.productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, executor);

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = this.productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String valueJson = JSON.toJSONString(skuValueIdsMap);
            result.put("valuesSkuJson", valueJson);
        }, executor);

        CompletableFuture<Void> spuPosterListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            // 获取海报信息
            List<SpuPoster> spuPosterList = this.productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
            result.put("spuPosterList", spuPosterList);
        }, executor);

        CompletableFuture<Void> skuAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<BaseAttrInfo> attrList = this.productFeignClient.getAttrList(skuId);
            List<Map<String, String>> skuAttrList = attrList.stream().map((baseAttrInfo) -> {
                Map<String, String> attrMap = new HashMap<>();
                attrMap.put("attrName", baseAttrInfo.getAttrName());
                attrMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return attrMap;
            }).collect(Collectors.toList());
            result.put("skuAttrList", skuAttrList);
        }, executor);

        // 多任务组合,所有的异步任务完成才算是完成
        CompletableFuture.allOf(priceCompletableFuture, skuInfoCompletableFuture, categoryViewCompletableFuture, spuSaleAttrListCompletableFuture, valuesSkuJsonCompletableFuture, spuPosterListCompletableFuture, skuAttrListCompletableFuture).join();
        return result;
    }

}
